package com.ct.application.shops.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.commons.vo.StatusRequest;
import com.ct.application.shops.convert.ShopsConvert;
import com.ct.application.shops.model.po.ShopsAddPO;
import com.ct.application.shops.model.po.ShopsQueryPO;
import com.ct.application.shops.model.po.ShopsUpdatePO;
import com.ct.application.shops.model.vo.ShopsListVO;
import com.ct.application.shops.model.vo.ShopsVO;
import com.ct.repository.commons.PageUtils;
import com.ct.shops.orm.Shops;
import com.ct.shops.repository.admin.IAdminShopsRepository;
import com.ct.utils.valid.ValidatorUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 门店表 服务实现类
 * </p>
 *
 * @author system
 */
@Service
public class ShopsService {

    @Resource
    private ShopsConvert shopsConvert;
    @Resource
    private IAdminShopsRepository adminShopsRepository;

    public boolean updateStatus(StatusRequest statusRequest){
        if(ValidatorUtil.isNumeric(statusRequest.getValue())) {
            return adminShopsRepository.updateStatus(statusRequest.getId(), statusRequest.getField(), Integer.parseInt(statusRequest.getValue()));
        }else{
            return adminShopsRepository.updateStatusStr(statusRequest.getId(), statusRequest.getField(), statusRequest.getValue());
        }
    }

    /**
     * 获取门店列表数据
     */
    public PageUtils<ShopsVO> getShopsList(ShopsQueryPO queryPO) {
        LambdaQueryWrapper<Shops> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(queryPO.getKeyword()), Shops::getName, queryPO.getKeyword());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getIsOperating()), Shops::getIsOperating, queryPO.getIsOperating());
        lambdaQueryWrapper.eq(!Objects.isNull(queryPO.getStatus()), Shops::getStatus, queryPO.getStatus());
        lambdaQueryWrapper.orderByDesc(Shops::getCreateTime);
        Page<Shops> page = new Page<>(queryPO.getPageNo(), queryPO.getPageSize());
        IPage<Shops> shopsIPage = adminShopsRepository.page(page, lambdaQueryWrapper);

        List<Shops> shopsList = shopsIPage.getRecords();
        if (shopsList.isEmpty()) {
            return PageUtils.getEmptyInstance();
        }
        List<ShopsVO> shopsVOList = shopsConvert.shopsListToShopsVOList(shopsList);
        return new PageUtils<>(shopsIPage, shopsVOList);
    }


    /**
     * 根据id查询门店
     */
    public ShopsVO getShopsById(Long id) {
        Shops shops = adminShopsRepository.getById(id);
        if (shops == null) {
            return null;
        }
        ShopsVO shopsVO = new ShopsVO();
        BeanUtils.copyProperties(shops,shopsVO);
        return shopsVO;
    }



    /**
     * 新增门店信息
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean add(ShopsAddPO shopsAddPO) {
        Shops shops = new Shops();
        BeanUtils.copyProperties(shopsAddPO, shops);
        return adminShopsRepository.save(shops);
    }


    /**
     * 修改门店信息
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean update(ShopsUpdatePO shopsUpdatePO) {
        Shops shops = new Shops();
        BeanUtils.copyProperties(shopsUpdatePO, shops);
        return shops.updateById();
    }


    /**
     * 删除门店
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return adminShopsRepository.removeById(id);
    }


    /**
     * 修改营业状态
     * @param id 主键
     * @param operatingStatus 是否营业 1是 0否
     */
    @Transactional(rollbackFor = Exception.class)
    public boolean updateOperatingStatus(Long id,Integer operatingStatus) {
        return adminShopsRepository.updateOperatingStatus(id, operatingStatus);
    }


    public List<ShopsListVO> findShopsList(){
        return shopsConvert.shopsListToShopsVOLists( adminShopsRepository.findShopsList());
    }
}
